package descentFly.away3D4.action
{
	import away3d.containers.ObjectContainer3D;
	
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	import space.sp_utils;
	
	public class df_dir extends df_OPAction {
		public static const FIELD_DIRECT	: String = 'dir';
		public static const DEFAULT_DIRECT	: Vector3D = new Vector3D(1, 0, 0);
		
		private var _dir: Vector3D;
		public function df_dir(a_options:Object, a_model:ObjectContainer3D) {
			super(a_options, a_model);
			checkOption(FIELD_DIRECT, DEFAULT_DIRECT);
		}
		
		override protected function update(percent: Number): void {
			var dir: Vector3D = options[FIELD_DIRECT].clone();
			dir.scaleBy(percent);
			objects.forEach(function (object: ObjectContainer3D, i: int, arr: Array): void {
				updateObject(i, object, dir);
			});
		}
		
		private function updateObject(index: int, object: ObjectContainer3D, curDir: Vector3D): void {
			var mat	: Matrix3D = originMatrix[index].clone();
			if (options.center) {
				var rMat 	: Matrix3D = sp_utils.lookAt(options.center, mat.transformVector(new Vector3D()));
				curDir = rMat.transformVector(curDir);
			}
			object.transform = updateMatrix(mat, curDir);
		}
		
		protected function updateMatrix(mat: Matrix3D, curDir: Vector3D): Matrix3D {
			mat.appendTranslation(curDir.x, curDir.y, curDir.z);
			return mat;
		}
	}
}